package site.param.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import site.dao.parameter.model.ParamMysql;
import site.param.init.JsonUtil;
import site.param.service.parameter.IParameterService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/1/3.
 */
@Controller
@RequestMapping("/param")
public class ParamController {
    @Autowired
    private IParameterService parameterService;

    @RequestMapping("/paramter")
    public String index(ModelMap map) {
        return "paramList";
    }

    @RequestMapping("/getAllPackageName")
    public void getAllPackageName(HttpServletRequest request, HttpServletResponse response) {
        print(request, response, parameterService.getPackageNames());
    }

    @RequestMapping("/getClassByPackageName")
    public void getAllClass(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "packageName", required = true) String packageName) {
        List<Map<String, String>> paramMysqlList = parameterService.getClassByPackage(packageName);
        print(request, response, paramMysqlList);
    }

    @RequestMapping("/getFiledValues")
    public void getFiledValues(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "serialVersionUid", required = true) String serialVersionUid) {
        List<ParamMysql> paramMysqlList = parameterService.getFiledValues(serialVersionUid);
        print(request, response, paramMysqlList);
    }

    @RequestMapping("/updateFiled")
    public void updateFiled(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", required = true) Long id,
            @RequestParam(value = "serialVersionUid", required = true) String serialVersionUid,
            @RequestParam(value = "filedValue", required = true) String filedValue) {
        ParamMysql paramMysql = new ParamMysql();
        paramMysql.setFiledValue(filedValue);
        paramMysql.setId(id);
        paramMysql.setSerialVersionUid(serialVersionUid);

        print(request, response, parameterService.updateParamter(paramMysql));
    }
    @RequestMapping(value = "/search" ,method = RequestMethod.GET)
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "keyValue", required = true) String keyValue) {
        print(request, response, parameterService.search(keyValue));
    }



    private void print(HttpServletRequest request, HttpServletResponse response, Object obj) {
        response.setCharacterEncoding("UTF-8");
        PrintWriter pr = null;
        try {
            String result = JsonUtil.toJSONString(obj);
            pr = response.getWriter();
            pr.print(result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (pr != null) {
                pr.close();
            }
        }
    }

}
